package com.dz.Incubator.service.impl;

import com.dz.Incubator.dao.ProblemDao;
import com.dz.Incubator.dto.ProblemDto;
import com.dz.Incubator.entity.ProblemInfoEntity;
import com.dz.Incubator.service.ProblemService;
import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.entity.ProblemEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProblemServiceImpl implements ProblemService {

    private static final Logger logger = LoggerFactory.getLogger(ProblemService.class);

    @Autowired
    private ProblemDao problemDao;

    @Override
    public ProblemEntity insert(ProblemEntity problemEntity) {
        problemDao.insert(problemEntity);

        return problemEntity;
    }

    @Override
    public ProblemEntity update(ProblemEntity problemEntity) {
        problemDao.update(problemEntity);

        return problemEntity;
    }

    @Override
    public Integer delete(Long id) {
        int ret = problemDao.delete(id);
        return ret;
    }

    @Override
    public List<ProblemEntity> find(Long[] ids) {
        List<ProblemEntity> datas = problemDao.find(ids);
        return datas;
    }

    @Override
    public ProblemEntity get(Long id) {
        ProblemEntity problemEntity = problemDao.get(id);
        return problemEntity;
    }

    @Override
    public List<ProblemEntity> findAll() {
        List<ProblemEntity> datas = problemDao.findAll();
        return datas;
    }

    @Override
    public Pagination findByPagination(Pagination pagination) {
        List<ProblemEntity> datas = problemDao.findByPagination(pagination);
        pagination.setList(datas);
        return pagination;
    }

    @Override
    public List<ProblemEntity> insertBatch(List<ProblemEntity> list) {
        int ret = problemDao.insertBatch(list);
        return list;
    }

    @Override
    public List<ProblemEntity> updateBatch(List<ProblemEntity> list) {
        int ret = problemDao.updateBatch(list);
        return list;
    }

    @Override
    public List<Long> deleteBatch(Long[] ids) {
        int ret = problemDao.deleteBatch(ids);
        List<ProblemEntity> datas = problemDao.find(ids);
        List<Long> list = new ArrayList<Long>();
        for (ProblemEntity data : datas) {
            list.add(data.getId());
        }
        return list;
    }

    /**
     * 查询常见问题信息
     *
     * @return
     */
    @Override
    public List<ProblemDto> getInfo() {
        return this.problemDao.getInfo();
    }

    /**
     * 通过服务查询问题列表
     *
     * @param serviceId
     * @return
     */
    @Override
    public List<ProblemDto> getInfoByService(Long serviceId) {
        return this.problemDao.getInfoByService(serviceId);
    }

    @Override
    public List<ProblemEntity> getTypeAndProblem() {
        List<ProblemEntity> problemEntityList = problemDao.getTypeAndList();
        for (ProblemEntity problemEntity : problemEntityList) {
            List<ProblemInfoEntity> list = problemEntity.getProblemList();
            if (list != null && list.size() > 3) {
                List<ProblemInfoEntity> result = list.subList(0, 2);
                problemEntity.setProblemList(result);
            }
        }
        return problemEntityList;
    }
}